-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[
    梅花 的生成控制器

    踏雪寻梅：在采集未被移植的浆果丛（多汁浆果丛）时，若当前环境温度低于0℃，有21%概率额外获得一朵梅花。
    berrybush berrybush2 berrybush_juicy 		
    self.inst:PushEvent("picked", { picker = picker, loot = loot, plant = self.inst })
    picker:PushEvent("picksomething", { object = self.inst, loot = loot })

    触发踏雪寻梅胡桃有台词：腊梅处处香，寻梅冻个疮/哇，是雪！（有语音）

    盐梅相成：敲碎盐矿时，有35%概率掉落一片梅花。梅花会漂在水上，可以放进盐盒。
    saltstack          self.inst:PushEvent("workfinished", { worker = worker })

    触发盐梅相成胡桃有台词：盐梅虽然相成，水火可别相济，我还得回去呢/这是一片新鲜的~盐渍梅花



    望梅止渴：夏天每次进入黄昏时，如果玩家所在位置附近没有冷火源和雪球发射器并且玩家雨露值为0，那么会在附近生成一朵煤花，同时玩家会凭空增加21雨露值。

    触发望梅止渴有台词：煮酒燃青煤，梅在酒中泣~/望煤而止渴，有如设重椁而止盗也。

]]--
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 浆果
    local berry_bush_prefabs = {
        ["berrybush"] = true,
        ["berrybush2"] = true,
        ["berrybush_juicy"] = true
    }
    local function berry_bush_picked_event_fn(inst,_table)
        local picker = _table and _table.picker
        if not (picker and picker.prefab == "hutao") then
            return
        end
        --- 移植过的不算数
        if inst.components.pickable and inst.components.pickable.transplanted == true then
            return
        end
        if not TUNING.HUTAO_DEBUGGING_MODE and (TheWorld.state.temperature > 0 or math.random() > 0.21 )then
            return
        end
        picker.components.inventory:GiveItem(SpawnPrefab("hutao_item_plum_blossom_petal"))
        picker.components.hutao_com_dialogue:Say("flower.got_plum_blossom_petal_from_berrybush")
        picker.SoundEmitter:PlaySound("dontstarve/common/together/celestial_orb/active")
    end
    local function berry_bush_hook_fn(inst)
        if not TheWorld.ismastersim then
            return
        end
        inst:ListenForEvent("picked",berry_bush_picked_event_fn)
    end
    for prefab,flag in pairs(berry_bush_prefabs) do
        AddPrefabPostInit(prefab,berry_bush_hook_fn)
    end
    -- AddPrefabPostInit(
    -- "hutao",
    -- function(inst)
    --     if not TheWorld.ismastersim then
    --         return
    --     end

    --     inst:ListenForEvent("picksomething",function(inst,_table)
    --         local object = _table and _table.object
    --         if not (object and berry_bush_prefabs[object.prefab]) then
    --             return
    --         end
    --         --- 移植过的不算数
    --         if object.components.pickable and object.components.pickable.transplanted == true then
    --             return
    --         end
    --         if not TUNING.HUTAO_DEBUGGING_MODE and (TheWorld.state.temperature > 0 or math.random() > 0.21 )then
    --             return
    --         end
    --         inst.components.inventory:GiveItem(SpawnPrefab("hutao_item_plum_blossom_petal"))
    --         inst.components.hutao_com_dialogue:Say("flower.got_plum_blossom_petal_from_berrybush")

    --     end)
    -- end)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[ 海盐矿
        盐梅相成：敲碎盐矿时，有35%概率掉落一片梅花。梅花会漂在水上，可以放进盐盒。
        saltstack
        self.inst:PushEvent("workfinished", { worker = worker })
        触发盐梅相成胡桃有台词：盐梅虽然相成，水火可别相济，我还得回去呢/这是一片新鲜的~盐渍梅花
    ]]
    local function saltstack_work_finished_event(inst,_table)
        local doer = _table and _table.worker
        if not ( doer and doer.prefab == "hutao") then
            return
        end
        if math.random() > 0.35 and not TUNING.HUTAO_DEBUGGING_MODE then
            return
        end
        if inst.components.lootdropper then
            inst.components.lootdropper:SpawnLootPrefab("hutao_item_plum_blossom_petal")
        end
        doer.components.hutao_com_dialogue:Say("flower.got_plum_blossom_petal_from_saltstack")
    end
    local function hook_saltstack(inst)
        if not TheWorld.ismastersim then
            return
        end
        inst:ListenForEvent("workfinished",saltstack_work_finished_event)
    end
    AddPrefabPostInit("saltstack",hook_saltstack)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[ 望梅止渴

        望梅止渴：夏天每次进入黄昏时，如果玩家所在位置附近(半径21）没有冷火源和雪球发射器并且玩家雨露值为0，那么会在附近（半径6）生成一朵煤花，同时玩家会凭空增加21雨露值。
        触发望梅止渴有台词：煮酒燃青煤，梅在酒中泣~/望煤而止渴，有如设重椁而止盗也。
        
    ]]--
    local FLOWER_SPAWN_WARNING_RADIUS = 21
    local FLOWER_SPAWN_RADIUS = 6
    local function find_firesuppressor(target,inst) --- 雪球机
        return target.prefab == "firesuppressor"
    end
    local function find_plantable_location(x,y,z)
        local ret_points = {}
        --- 创建虚拟蝴蝶
        local visual_butterfly = SpawnPrefab("butterfly")
        visual_butterfly:Hide()
        visual_butterfly.Transform:SetPosition(x,y,z)
        ---- 按照距离 -2 进行环状查找
        for temp_radius = FLOWER_SPAWN_RADIUS,0,-2 do
            if temp_radius > 0 then
                local temp_points = TUNING.HUTAO_FNS:GetSurroundPoints({
                    target = Vector3(x,y,z),
                    range = temp_radius,
                    num = temp_radius*3,
                })
                for _,pt in ipairs(temp_points) do
                    if TheWorld.Map:CanDeployAtPoint(pt,visual_butterfly) then
                        table.insert(ret_points,pt)
                    end
                end
            end
        end
        ---- 删除虚拟蝴蝶
        visual_butterfly:Remove()
        ---- 随机一个坐标
        if #ret_points > 0 then
            return ret_points[math.random(#ret_points)]
        end
        return Vector3(x,y,z)
    end
    local function start_flower_spawn_task(inst)
        if inst.components.hutao_com_flower_switcher:IsNearColdSource(FLOWER_SPAWN_WARNING_RADIUS) then
            return
        end
        if inst.components.hutao_com_flower_switcher:IsNearColdSource(TUNING.FIRE_DETECTOR_RANGE,find_firesuppressor) then
            return
        end
        if inst.components.moisture:GetMoisture() > 0 then
            return
        end
        local x,y,z = inst.Transform:GetWorldPosition()
        local pt = find_plantable_location(x,y,z)
        SpawnPrefab("hutao_plant_coal_flower").Transform:SetPosition(pt.x,0,pt.z)
        SpawnPrefab("splash").Transform:SetPosition(pt.x,0,pt.z)
        inst.components.hutao_com_dialogue:Say("flower.coal_flower_spawned_in_summer_dusk")
        inst.components.moisture:DoDelta(21)
    end
    AddPrefabPostInit(
        "hutao",
        function(inst)
            if not TheWorld.ismastersim then
                return
            end
            inst:AddComponent("hutao_com_flower_switcher")

            inst:WatchWorldState("isdusk",function()
                inst:DoTaskInTime(5,function()
                    if TheWorld.state.isdusk and TheWorld.state.issummer or TUNING.HUTAO_DEBUGGING_MODE then
                        start_flower_spawn_task(inst)
                    end
                end)
            end)

        end
    )
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





